package com.ruoyi.business.controller;

import com.ruoyi.business.dto.InventoryDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Inventory;
import com.ruoyi.business.service.IInventoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * InventoryController
 *
 * @author liutietou
 * @since 1.0 2025-06-22
 */
@RestController
@RequestMapping("/business/Inventory")
public class InventoryController extends BaseController {
    @Autowired
    private IInventoryService inventoryService;

    /**
     * 查询Inventory列表
     */
    @PreAuthorize("@ss.hasPermi('business:Inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(InventoryDto inventory) {
        if(inventory.getNum()==null){
            inventory.setNum(BigDecimal.ZERO);
        }
        Page<InventoryDto> page = inventoryService.findInventoryPage(inventory);
        return getDataTable(page);
    }

    /**
     * 导出Inventory列表
     */
    @PreAuthorize("@ss.hasPermi('business:Inventory:export')")
    @Log(title = "Inventory", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Inventory inventory) {
        List<Inventory> list = inventoryService.findInventoryList(inventory);
        ExcelUtil<Inventory> util = new ExcelUtil<>(Inventory.class);
        return util.exportExcel(list, "Inventory");
    }

    /**
     * 获取Inventory详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:Inventory:query')")
    @GetMapping(value = "/{cinvcode}")
    public AjaxResult getInfo(@PathVariable("cinvcode") String cinvcode) {
        return AjaxResult.success(inventoryService.findById(cinvcode));
    }

    /**
     * 新增Inventory
     */
    @PreAuthorize("@ss.hasPermi('business:Inventory:add')")
    @Log(title = "Inventory", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Inventory inventory) {
        inventoryService.save(inventory);
        return AjaxResult.success();
    }

    /**
     * 修改Inventory
     */
    @PreAuthorize("@ss.hasPermi('business:Inventory:edit')")
    @Log(title = "Inventory", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Inventory inventory) {
        inventoryService.save(inventory);
        return AjaxResult.success();
    }

    /**
     * 删除Inventory
     */
    @PreAuthorize("@ss.hasPermi('business:Inventory:remove')")
    @Log(title = "Inventory", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cinvcodes}")
    public AjaxResult remove(@PathVariable String[] cinvcodes) {
        inventoryService.deleteByIds(Arrays.asList(cinvcodes));
        return AjaxResult.success();
    }
}
